<template>
	<view class="bg-white" style="min-height: 100vh;padding: 0 40rpx;"
		:style="{background:'url('+Imgbase+'/brave_goodindex_bgc.png)','background-size':'100%',}">

		<view class="padding-top-90">
			<image @click="back" class="w-60 h-120 margin-right-20" :src="Imgbase + '/kp_setting_back.png'"
				mode="aspectFit"></image>
			<image class="w-200 h-120" :src="Imgbase + '/kp_add_title.png'" mode="aspectFit"></image>
		</view>

		<!-- 名字 -->
		<view class="margin-top-20 w100 h-100 item flex-between">
			<image class="w100 h-100 item_img" :src="Imgbase + '/kp_add_img1.png'" mode="aspectFit"></image>
			<view class="">

			</view>
			<input class="input_one padding-right-35" type="text" placeholder="名字">
		</view>

		<!-- 手机号 -->
		<view class="margin-top-15 w100 h-100 item flex-between">
			<image class="w100 h-100 item_img" :src="Imgbase + '/kp_add_img2.png'" mode="aspectFit"></image>
			<view class="">

			</view>
			<input class="input_one padding-right-35" type="text" placeholder="手机号">
		</view>

		<!-- 省、市、区 -->
		<view class="margin-top-15 w100 h-100 item flex-between">
			<image class="w100 h-100 item_img" :src="Imgbase + '/kp_add_img3.png'" mode="aspectFit"></image>
			<view class="">

			</view>
			<input class="input_one padding-right-35" type="text" placeholder="省、市、区">
		</view>

		<!-- 详细地址 -->
		<view class="margin-top-15 w100 h-100 item flex-between">
			<image class="w100 h-100 item_img" :src="Imgbase + '/kp_add_img4.png'" mode="aspectFit"></image>
			<view class="">

			</view>
			<input class="input_one padding-right-35" type="text" placeholder="详细地址">
		</view>

		<view class="margin-top-15 w100 h-100 item flex-between">
			<image class="w100 h-100 item_img" :src="Imgbase + '/kp_add_img5.png'" mode="aspectFit"></image>
			<view class="">

			</view>
			<view class="padding-right-35">
				<u-switch inactiveColor="#333" v-model="value" @change="change"></u-switch>
			</view>
		</view>

		<view class="margin-top-150">
			<image class="w100 h-100" :src="Imgbase + '/kp_add_img6.png'" mode="aspectFit"></image>
		</view>


		<view class="btn w100 h-200 text-center">
			<image class="img h-155" :src="Imgbase + '/kp_add_btn1.png'" mode="aspectFit"></image>
		</view>
		<view class="content" v-if="add == 1">
			<auto-parse-address boxClass="c-box" @result="result"></auto-parse-address>
		</view>


		<view class="h-100">

		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				Imgbase: getApp().globalData.Imgbase,
				addlist: [],
				show: false,
				itemlist: [{
						text: '收货人',
						prs: '名字',
						data: ''
					},
					{
						text: '手机号码',
						prs: '手机号',
						data: ''
					},
					{
						text: '详细地址',
						prs: '省、市、区、街道',
						data: ''
					},
				],
				value: false,
				selected: 0,
				id: '',
				modify: 0,
			}
		},
		onShow() {
			this.addressList()
		},
		onLoad(options) {},
		methods: {
			result(e) {
				console.log(e)
			},
			back() {
				uni.navigateBack()
			},
			// 删除地址
			deletes() {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该地址',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: _this.$globals.apibase + '/api/Luckorder/deleteAddress',
								data: {
									token: uni.getStorageSync('token'),
									id: _this.id,
								},
								method: 'POST',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									_this.addressList()
									_this.show = false
									this.$refs.uToast.show({
										message: "地址删除成功",
									})
								}
							});
						} else if (res.cancel) {}
					}
				});
			},
			opens() {
				this.show = true
				this.id = ''
				this.itemlist.forEach(function(item, index) {
					item.data = ''
				})
				this.selected = 0
				this.value = false
				// console.log('open');
			},
			open() {

			},
			close() {
				this.show = false
				// console.log('close');
			},
			change(e) {
				if (e) {
					this.selected = 1
				} else {
					this.selected = 0
				}
			},
			// 编辑地址
			modifyadd(idx) {
				// 判断是否为编辑
				this.modify = 1
				this.show = true
				this.itemlist[0].data = this.addlist[idx].name
				this.itemlist[1].data = this.addlist[idx].phone
				this.itemlist[2].data = this.addlist[idx].detail
				this.id = this.addlist[idx].id
				console.log(this.addlist[idx].selected)
				if (this.addlist[idx].selected == 1) {
					this.selected = 1
					this.value = true
				} else {
					this.selected = 0
					this.value = false
				}
			},
			// 保存收货地址
			addresson() {
				let _this = this
				if (_this.itemlist[0].data == '') {
					this.$refs.uToast.show({
						message: "请填写收货人姓名",
					})
					return
				}
				if (_this.itemlist[1].data.length != 11) {
					this.$refs.uToast.show({
						message: "请填写正确的手机号码",
					})
					return
				}
				if (_this.itemlist[2].data == '') {
					this.$refs.uToast.show({
						message: "请填写详细地址",
					})
					return
				}

				uni.request({
					url: _this.$globals.apibase + '/api/Luckorder/operaAddress',
					data: {
						token: uni.getStorageSync('token'),
						name: _this.itemlist[0].data,
						phone: _this.itemlist[1].data,
						detail: _this.itemlist[2].data,
						selected: _this.selected,
						id: _this.id,
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.code == '1') {
							_this.addressList()
							_this.show = false
							if (_this.id == 0) {
								this.$refs.uToast.show({
									message: "添加成功",
								})
							} else {
								this.$refs.uToast.show({
									message: "修改成功",
								})
							}
						}
						_this.itemlist.forEach(function(item, index) {
							item.data = ''
						})
						_this.selected = 0
						_this.value = false
					}
				});
			},
			// 获取地址列表
			addressList() {
				let _this = this;
				uni.request({
					url: _this.$globals.apibase + '/api/Luckorder/addressList',
					data: {
						token: uni.getStorageSync('token'),
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						_this.addlist = res.data.data.list;
						// for (let i = 0; i < res.data.data.list.length; i++) {
						// 	_this.addresslist = _this.addresslist.concat(res.data.data.list[i])
						// }
						// if (_this.address == null) {
						// 	_this.address = res.data.data.list2[0]
						// 	_this.address_id = res.data.data.list2[0].id
						// }
					}
				});
			},
		},
	}
</script>

<style lang="less" scoped>
	.item {
		position: relative;

		.item_img {
			position: absolute;
		}

		.input_one {
			text-align: right;
		}
	}

	.btn {
		position: fixed;
		bottom: 20rpx;
		left: 0;

		.img {
			width: 90%;
		}
	}

	.black {
		background-color: #353941;
	}

	.org {
		background-color: #FC8924;
	}
</style>